
package clases;
import java.awt.Color;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.xml.transform.Result;

public class IEmpleado extends javax.swing.JPanel {

    Statement ST;
    Ventana principal;
    
    public IEmpleado(Ventana aThis) {
        initComponents();
        principal = aThis;
        this.setBounds(0, 0, 700, 500);
        
    }

    IEmpleado(Connection conector) {
        initComponents();
        this.setBounds(0,0,500,500);
     
    }

  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setBackground(new java.awt.Color(170, 198, 205));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setFont(new java.awt.Font("Broadway", 1, 18)); // NOI18N
        jLabel1.setText("Compu System/");

        jLabel2.setFont(new java.awt.Font("Lucida Bright", 1, 14)); // NOI18N
        jLabel2.setText("Ventas");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Descripcion", "Costo", "Disponible"
            }
        ));
        jTable1.setColumnSelectionAllowed(true);
        jScrollPane1.setViewportView(jTable1);

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });
        jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField2KeyTyped(evt);
            }
        });

        jLabel4.setText("Producto");

        jButton2.setText("buscar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("borrar Contenido");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("hacer compra");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1)
                        .addGap(6, 6, 6)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(13, 13, 13)
                                .addComponent(jButton3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel4)
                                .addGap(26, 26, 26)
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(83, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel2)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)))
                .addGap(21, 21, 21)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addContainerGap(87, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
       
        
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        
        
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        try {
            // TODO add your handling code here:
         ResultSet prod = ST.executeQuery("Select id_producto as id,describe_producto as d,Costo_unitario as costo,cantidad from producto where id_producto = '"+
         jTextField1.getText()+"'");
            
            
         while (prod.next()) {
                String[] datos = {prod.getString("id"),prod.getString("d"),prod.getString("costo"),prod.getString("cantidad")};
                modelo.addRow(datos);   
            }
        } catch (SQLException ex) {
            Logger.getLogger(IEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        }

        jTable1.setModel(modelo);
        
        
        
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTextField2KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField2KeyTyped
        // TODO add your handling code here:
        
         if (!Character.isDigit(evt.getKeyChar()) && !Character.isISOControl(evt.getKeyChar())) {
            Toolkit.getDefaultToolkit().beep();//manda un sonido de error
            evt.consume();//consume el key event. y no pasa nada al jtextfield
        }

        if (evt.getKeyChar() == KeyEvent.VK_ENTER && jTextField2.getText().length() > 0) {
            DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
           // for(int i =0 ; i<jTable1.getRowCount();i++)
             //   modelo.removeRow(i);
            
             try {
                 // TODO add your handling code here:
                 ResultSet prod = ST.executeQuery("Select id_producto as id,describe_producto as d,Costo_unitario as costo,cantidad from producto where id_producto = '"+
                         jTextField2.getText()+"'");
                 
                 while (prod.next()) {
                     
                     String[] datos = {prod.getString("id"),prod.getString("d"),prod.getString("costo"),prod.getString("cantidad")};
                     int cant = prod.getInt("cantidad");
                     if(cant > 0)
                     modelo.addRow(datos);
                 }
             } catch (SQLException ex) {
                 Logger.getLogger(IEmpleado.class.getName()).log(Level.SEVERE, null, ex);
             }
             jTable1.setModel(modelo);
             jTextField2.setText("");
        }
        
         
        
        
         
        
        
    }//GEN-LAST:event_jTextField2KeyTyped

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
        // TODO add your handling code here:

         if (!Character.isDigit(evt.getKeyChar()) && !Character.isISOControl(evt.getKeyChar())) {
            Toolkit.getDefaultToolkit().beep();//manda un sonido de error
            evt.consume();//consume el key event. y no pasa nada al jtextfield
        }

        if (evt.getKeyChar() == KeyEvent.VK_ENTER && jTextField1.getText().length() > 0) {
            DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
            int cFilas = jTable1.getRowCount();
            for(int i =0 ; i<cFilas;i++)
                modelo.removeRow(0);
            
             try {
                 // TODO add your handling code here:
                 ResultSet prod = ST.executeQuery("Select id_producto as id,describe_producto as d,Costo_unitario as costo,cantidad from producto where id_producto = '"+
                         jTextField1.getText()+"'");
                 
                 while (prod.next()) {
                     String[] datos = {prod.getString("id"),prod.getString("d"),prod.getString("costo"),prod.getString("cantidad")};
                     modelo.addRow(datos);
                 }
             } catch (SQLException ex) {
                 Logger.getLogger(IEmpleado.class.getName()).log(Level.SEVERE, null, ex);
             }
             jTable1.setModel(modelo);
             jTextField1.setText("");
        }
        
         
        
        
    }//GEN-LAST:event_jTextField1KeyTyped

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        
         DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
           int cFila= jTable1.getRowCount();
         for(int i =0 ; i<cFila;i++)
                modelo.removeRow(0);
        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:

        /*
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
         
            int cFila = jTable1.getRowCount();
            for(int i =cFila ; i>0;i--){
               // int id = Integer.parseInt(jTable1.getComponentAt(0, (i-1)).toString());
                int id = Integer.parseInt(jTable1.getValueAt(0, (i-1)).toString());
                
             try {
                    
                    ResultSet r = ST.executeQuery("select id_producto,cantidad from producto where id_producto = '"+id+"'");
                    int disponible = Integer.parseInt(r.getString("cantidad"));
                    
                    System.out.print("id: "+id+"    diponible = "+disponible);
                    ST.executeUpdate("update producto set cantidad = '"+(disponible-1)+"' where id_producto = '"+id+"'");
                    
                    r = ST.executeQuery("select cantidad_maxima,cantidad  from producto where id_producto = '"+id+"'");
                    int acomprar = r.getInt("cantidad_maxima") - r.getInt("cantidad");
                    
                    System.out.println("acomprar:"+acomprar );
                    if( 5  == disponible ){
                       ST.executeUpdate("insert producto_por_terminar(cantidad,producto) values('"+acomprar+"','"+id+"')");
                    }
                    else{
                        if(5>disponible){
                            ST.executeUpdate("update producto_por_terminar set cantidad = '"+acomprar+"' where producto= '"+id+"' ");
                        }
                    }
                    
                 
                 
             } catch (SQLException ex) {
                 Logger.getLogger(IEmpleado.class.getName()).log(Level.SEVERE, null, ex);
             }
            }
                
            
        //    modelo.removeRow(i);
        
        
        */
        
        
    }//GEN-LAST:event_jButton4ActionPerformed

    public static void main(String args[]) throws InstantiationException, IllegalAccessException{
    }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

    void setStatement(Statement stmt) {
       
        ST = stmt;
        
    }
}
